package com.chendx.store.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.PageResponseResult;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.goods.dto.SpecDto;
import com.chendx.model.goods.pojo.Category;
import com.chendx.model.goods.pojo.Spec;
import com.chendx.store.goods.mapper.CategoryMapper;
import com.chendx.store.goods.mapper.SpecMapper;
import com.chendx.store.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chendx on 2022/4/19.
 */
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper,Spec> implements SpecService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseResult loadSpec(SpecDto dto) {

        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        LambdaQueryWrapper<Spec> queryWrapper = new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(dto.getName())){
            queryWrapper.eq(Spec::getName,dto.getName());
        }
        if (!StringUtils.isEmpty(dto.getStatus())){
            queryWrapper.eq(Spec::getStatus,dto.getStatus());
        }
        if (!StringUtils.isEmpty(dto.getTemplateId())){
            queryWrapper.eq(Spec::getTemplateId,dto.getTemplateId());
        }
        dto.setSize(30);
        Page ipage = new Page(dto.getPage(),dto.getSize());
        IPage<Spec> result = page(ipage, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }



    @Override
    public ResponseResult loadByCategory(Integer categoryId) {
        if (StringUtils.isEmpty(categoryId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<Category> categoryBrands = categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getId, categoryId));
        List<Integer> templateIds = categoryBrands.stream().map(x -> x.getTemplateId()).collect(Collectors.toList());
        List<Spec> specList = list(Wrappers.<Spec>lambdaQuery().in( Spec::getTemplateId, templateIds).eq(Spec::getStatus,"1"));
        return ResponseResult.okResult(specList);
    }

    @Override
    public ResponseResult one(Integer id) {

        return null;
    }

    @Override
    public ResponseResult add(SpecDto dto) {
        if (dto == null || dto.getName() == null || dto.getOptions() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Spec spec = new Spec();
        List<String> optionsList = dto.getOptions();
        String options = JSON.toJSONString(optionsList).replace("[", "").replace("]", "").replace("\"","");
        spec.setName(dto.getName());
        spec.setOptions(options);
        spec.setStatus(dto.getStatus());
        save(spec);
        return ResponseResult.okResult(spec);
    }

    @Override
    public ResponseResult findNoTemplateList() {
        List<Spec> list = list(Wrappers.<Spec>lambdaQuery().isNull(Spec::getTemplateId));
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult loadByCid(Integer categoryId) {
        if (StringUtils.isEmpty(categoryId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<Category> categoryBrands = categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getId, categoryId));
        List<Integer> templateIds = categoryBrands.stream().map(x -> x.getTemplateId()).collect(Collectors.toList());
        List<Spec> specList = list(Wrappers.<Spec>lambdaQuery().in( Spec::getTemplateId, templateIds));
        return ResponseResult.okResult(specList);
    }

}
